package currentment.printOrder;

public class printOddAndEven implements Runnable {

    private static int count = 0;
    private static final Object o = new Object();

    @Override
    public void run() {
        while (count <= 100) {
            synchronized (o) {
                System.out.println(Thread.currentThread().getName() + ":" + count++);
                // 立马唤醒其它线程
                o.notify();
                if (count <= 100) {
                    try {
                        // 自己处于等待状态
                        o.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Runnable run = new printOddAndEven();
        Thread t1 = new Thread(run, "Odd");
        t1.start();
        Thread.sleep(100);

        Thread t2 = new Thread(run, "Even");
        t2.start();
    }
}
